#include "tree.h"
#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

void PreorderTraversal( BinTree BT ); /* 先序遍历，由裁判实现，细节不表 */
void InorderTraversal( BinTree BT );  /* 中序遍历，由裁判实现，细节不表 */
void PostorderTraversal( BinTree BT ); /*后序遍历*/
void LevelorderTraversal( BinTree BT ); /*层次遍历*/
//最近公共祖先
Position LowestCommonAncestor(BinTree BST,ElementType X,ElementType Y);

BinTree Insert( BinTree BST, ElementType X );
BinTree Delete( BinTree BST, ElementType X );
Position Find( BinTree BST, ElementType X );
Position FindMin( BinTree BST );
Position FindMax( BinTree BST );

int main()
{
    BinTree BST, MinP, MaxP, Tmp;
    ElementType X;
    int N, i;

    BST = NULL;
    scanf("%d", &N);
    for ( i=0; i<N; i++ ) {
        scanf("%d", &X);
        BST = Insert(BST, X);
    }
    printf("Preorder:"); PreorderTraversal(BST); printf("\n");
    MinP = FindMin(BST);
    MaxP = FindMax(BST);
    scanf("%d", &N);
    for( i=0; i<N; i++ ) {
        scanf("%d", &X);
        Tmp = Find(BST, X);
        if (Tmp == NULL){
            printf("%d is not found\n", X);
        }else {
            printf("%d is found\n", Tmp->Data);
            if (Tmp==MinP){
                printf("%d is the smallest key\n", Tmp->Data);
            }
            if (Tmp==MaxP){
                printf("%d is the largest key\n", Tmp->Data);
            }
        }
    }
    scanf("%d", &N);
    for( i=0; i<N; i++ ) {
        scanf("%d", &X);
        BST = Delete(BST, X);
    }
    printf("Inorder:"); InorderTraversal(BST); printf("\n");

    return 0;

}
/* 你的代码将被嵌在这里 */


BinTree Insert( BinTree BST, ElementType X ){
    BinTree  p = BST;
    if (p == NULL){
        p=(BinTree) malloc(sizeof(struct TNode));
        p->Left=NULL;
        p->Right=NULL;
        p->Data=X;
        return p;
    } else{
        if (X > p->Data){
            p-> Right = Insert(p->Right,X);
        } else if (X < p->Data){
            p-> Left = Insert(p->Left,X);
        }
        return p;
    }
}


BinTree Delete(BinTree BST, ElementType X ){
    BinTree p = BST;
    if (p == NULL){
        printf("Not Found\n");
        return p;
    }
    if (X > p->Data){
        p->Right=Delete(p->Right,X);
    } else if (X < p->Data){
        p->Left=Delete(p->Left,X);
    } else{
        if (p -> Left == NULL){
            BinTree q=p->Right;
            free(p);
            return q;
        } else if (p -> Right ==NULL){
            BinTree q=p->Left;
            free(p);
            return q;
        } else{
            BinTree  q=p->Left;
            while(q->Right->Right != NULL){
                q = q->Right;
            }
            BinTree r=q->Right;
            q->Right=r->Left;
            //将左子树最右节点和查找到的节点进行互换
            r->Left=p->Left;
            r->Right=p->Right;
            free(p);
            return r;
        }
    }
    return p;
}

Position LowestCommonAncestor(BinTree BST,ElementType X,ElementType Y){
    if(BST == NULL){
        return BST;
    }
    BinTree p=BST;
//    if (X > p->Data && Y > p->Data){
//        return LowestCommonAncestor(p->Right,X,Y);
//    }else if (X < p->Data && Y  < p->Data){
//        return LowestCommonAncestor(p->Left,X,Y);
//    } else{
//        return p;
//    }
//迭代法
    while (p != NULL){
        if (X > p->Data && Y > p->Data){
            p=p->Right;
        }else if (X < p->Data && Y  < p->Data){
            p=p->Left;
        } else{
            return p;
        }
    }
    return NULL;
}

Position Find(BinTree BST, ElementType X ){
    Position  p = BST;
    if (p == NULL){
        return p;
    }
    if (X < p->Data){
        return Find(p->Left,X);
    } else if (X > p->Data){
        return Find(p->Right,X);
    } else if (p->Data == X){
        return p;
    }
}

Position FindMin(BinTree BST ){
    if (BST == NULL ){
        return BST;
    }
    Position p =BST;
    while (p->Left != NULL){
        p =p->Left;
    }
    return p;
}

Position FindMax(BinTree BST ){
    if (BST == NULL ){
        return BST;
    }
    Position p =BST;
    while (p->Right != NULL){
        p =p->Right;
    }
    return p;
}

void InorderTraversal(BinTree BT ){
    if (BT != NULL){
        InorderTraversal(BT->Left);
        printf("%d ",BT->Data);
        InorderTraversal(BT->Right);
    }

}

void PostorderTraversal(BinTree BT ){
    if(BT!=NULL){
        printf("%d ",BT->Data);
        PreorderTraversal(BT->Left);
        PreorderTraversal(BT->Right);
    }
}


void PreorderTraversal(BinTree BT ){
    //前序遍历
    if(BT!=NULL){
        PreorderTraversal(BT->Left);
        PreorderTraversal(BT->Right);
        printf("%d ",BT->Data);
    }
}

void LevelorderTraversal(BinTree BT ){
    if(BT == NULL){
        return;
    }
    BinTree queue[100001];
    int head=0,rear=0;
    queue[rear++]=BT;
    while (head != rear){
        BinTree  tmp=queue[head++];
        printf(" %c",tmp->Data);
        if(tmp->Left != NULL){
            queue[rear++]=tmp->Left;
        }
        if(tmp->Right != NULL){
            queue[rear++]=tmp->Right;
        }
    }

}
